package com.example.easycharge.service;

import com.example.easycharge.entity.Configuration;
import com.example.easycharge.notRepEntity.WaitArea;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ConfService {
    private long oldConfId;
    @Autowired
    private ChargeService chargeService;
    @Autowired
    private BillService billService;

    private WaitArea waitArea = WaitArea.getInstance();

    public boolean setNewConf(Configuration configuration) {
        oldConfId = configuration.getConfigurationID();
        //
        chargeService.shrinkQueue(configuration.getQueueLen());
        chargeService.shrinkArea(configuration.getAreaSize());
        chargeService.setBaseStrategyEnum(configuration.getBaseStrategy());
        chargeService.setMIN_SIG_SIZE(configuration.getSigSize());
        chargeService.setErrorStrategyEnum(configuration.getErrorStrategy());

        //
        billService.setNORMAL_PRICE(configuration.getBasePrice());
        billService.setPEAK_PRICE(configuration.getPeakPrice());
        billService.setBOTTOM_PRICE(configuration.getBottomPrice());
        billService.setSERVICE_PRICE(configuration.getServicePrice());
        billService.setSECTION(configuration.getSection());
        billService.setOvertimeMoneyPreHour(configuration.getOvertimeMoneyPreHour());
        //
        waitArea.setLatitudeMAX(configuration.getLatitudeMAX());
        waitArea.setLatitudeMIN(configuration.getLatitudeMIN());
        waitArea.setLongitudeMax(configuration.getLongitudeMax());
        waitArea.setLongitudeMIN(configuration.getLongitudeMIN());
        return true;
    }

    public Configuration getNowConf() {
        Configuration configuration = new Configuration();
        configuration.setConfigurationID(oldConfId);

        configuration.setQueueLen(chargeService.getMAX_QUEUE_LENGTH());
        configuration.setAreaSize(chargeService.getMAX_AREA_SIZE());
        configuration.setBaseStrategy(chargeService.getBaseStrategyEnum());
        configuration.setErrorStrategy(chargeService.getErrorStrategyEnum());
        configuration.setSigSize(chargeService.getMIN_SIG_SIZE());

        configuration.setBasePrice(billService.getNORMAL_PRICE());
        configuration.setPeakPrice(billService.getPEAK_PRICE());
        configuration.setBottomPrice(billService.getBOTTOM_PRICE());
        configuration.setServicePrice(billService.getSERVICE_PRICE());
        configuration.setSection(billService.getSECTION());
        configuration.setOvertimeMoneyPreHour(billService.getOvertimeMoneyPreHour());

        configuration.setLatitudeMAX(waitArea.getLatitudeMAX());
        configuration.setLatitudeMIN(waitArea.getLatitudeMIN());
        configuration.setLongitudeMax(waitArea.getLongitudeMax());
        configuration.setLongitudeMIN(waitArea.getLongitudeMIN());

        return configuration;
    }
}
